/**
 * author: KujoMiva
 * discription: egret utils package
 * 想整理又不想整理...  还是做项目的时候慢慢累积吧 = = 
 */
class Utils {
    /**
     * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
     */
    public static createBitmapByName(name: string): egret.Bitmap {
        const result: egret.Bitmap = new egret.Bitmap()
        const texture: egret.Texture = RES.getRes(name)
        result.texture = texture
        return result
    }
    // 锚点居中, 传入一个显示对象, 设置它的锚点居中到自己正中心
    public static anchorCenter(item: egret.DisplayObject): void {
        item.anchorOffsetX = item.width / 2
        item.anchorOffsetY = item.height / 2
    }
    // 取消事件冒泡
    public static superStopPropagation(evt: egret.TouchEvent): void {
        evt.stopPropagation()
        evt.stopImmediatePropagation()
    }
    /**
     * 搭配html页面的,截图分享
     */
    public static shareImage(target: egret.DisplayObject): void {
        var renderTexture = new egret.RenderTexture();
        renderTexture.drawToTexture(target);//渲染到临时画布
        var divImage = document.getElementById("divImage");//获取DIV
        var shareImage: HTMLImageElement = document.getElementById("shareImage") as HTMLImageElement;//获取Image标签
        shareImage.src = renderTexture.toDataURL('image/jpeg');//把数据赋值给Image
        divImage.style.display = "block";//显示DIV
    }
    /**
     * 显示对象的显示属性设置
     */
    public static displayObjectSetting(params: {
        target: egret.DisplayObject,
        width?: number,
        height?: number,
        x?: number,
        y?: number,
        zIndex?: number,
    }): void {
        const {target, width, height, x = 0, y = 0, zIndex} = params
        if (width) {
            target.width = width
        }
        if (height) {
            target.height = height
        }
        if (zIndex) {
            target.zIndex = zIndex
        }
        target.x = x
        target.y = y
    }
    /** 拖动事件 */
    public static dragDisplayObject(params: {
        target: egret.DisplayObject,
        container: egret.DisplayObjectContainer,
        that: egret.DisplayObjectContainer,
        funObj?: {
            startFun?: Function,
            moveFun?: Function,
            endFun?: Function
        },
        isDrag?: boolean,
        isLog?: boolean
    }): void {
        // 拿取参数
        const { target, container, that: miva, funObj = {}, isDrag = true, isLog = true } = params
        // 拿取附加函数
        const {startFun, moveFun, endFun } = funObj
        // 初始触摸点
        const startPoint: egret.Point = new egret.Point()
        // 计算位置后触摸点
        const computePoint: egret.Point = new egret.Point()
        // 开启视图对象的触摸
        target.touchEnabled = true
        target.addEventListener(egret.TouchEvent.TOUCH_BEGIN, touchBeginHandler, container)
        // 触摸开始事件处理器
        function touchBeginHandler(evt: egret.TouchEvent) {
            startPoint.x = evt.stageX
            startPoint.y = evt.stageY
            miva.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, touchMoveHandler, container)
            miva.stage.once(egret.TouchEvent.TOUCH_END, touchEndHandler, container)
            // 触摸开始时把触摸对象置顶
            try {
                target.zIndex = 99 //egret 5.2.24 +
            } catch (err) {
                container.setChildIndex(target, 99) //egret 5.2.24 -
            }
            // 判断执行触摸开始的附加方法
            if (startFun) {
                startFun({ evt, target, container, startPoint })
            }
        }
        // 拖动处理器
        function touchMoveHandler(evt: egret.TouchEvent) {
            // 这里计算
            computePoint.x = evt.stageX - startPoint.x
            computePoint.y = evt.stageY - startPoint.y
            startPoint.x = evt.stageX
            startPoint.y = evt.stageY
            // 拖动开关
            if (isDrag) {
                target.y += computePoint.y
                target.x += computePoint.x
            }
            // 判断执行触摸移动的附加方法
            if (moveFun) {
                moveFun({ evt, target, container, startPoint, computePoint })
            }
        }
        // 结束触摸处理器
        function touchEndHandler(evt: egret.TouchEvent) {
            miva.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, touchMoveHandler, container)
            // 输出坐标开关
            if (isLog) {
                console.log(`x:%s, y:%s`, target.x, target.y)
            }
            // 判断执行触摸结束的附加方法
            if (endFun) {
                endFun({ evt, target, container, startPoint })
            }
        }
    }
    /** 缩放添加视图对象 */
    public static createScale(target, container: egret.DisplayObjectContainer) {
        container.addChild(target);
        // target.scaleX = .5;
        // target.scaleY = .5;
    }
    /** 创建提示点 */
    public static createTipPoint(): egret.Sprite {
        const tipPoint = new egret.Sprite();
        tipPoint.graphics.beginFill(0xffffff, 1);
        tipPoint.graphics.drawCircle(0, 0, 10)
        tipPoint.graphics.endFill();
        tipPoint.graphics.beginFill(0xffffff, .3);
        tipPoint.graphics.drawCircle(0, 0, 20)
        tipPoint.graphics.endFill();
        tipPoint.graphics.lineStyle(3, 0xffffff);
        tipPoint.graphics.drawArc(0, 0, 20, 0, Math.PI / 180 * 60, true);
        tipPoint.graphics.endFill();

        egret.Tween.get(tipPoint, { loop: true })
            .to({ rotation: 180, scaleX: .5, scaleY: .5 }, 1000)
            .to({ rotation: 360, scaleX: 1, scaleY: 1 }, 1000);

        return tipPoint;
    }
    /** 道具提示动画 */
    public static itemAniamtion(target) {
        // Main.anchorCenter(target);
        egret.Tween.get(target, { loop: true })
            .to({ scaleX: target.scaleX * .8, scaleY: target.scaleY * .8 }, 500)
            .to({ scaleX: target.scaleX * 1.2, scaleY: target.scaleY * 1.2 }, 500)
            .to({ scaleX: target.scaleX, scaleY: target.scaleX }, 500)
            .wait(500)
            .to({ rotation: target.rotation + 10 }, 500)
            .to({ rotation: target.rotation + -10 }, 500)
            .to({ rotation: target.rotation + 10 }, 500)
            .to({ rotation: target.rotation + 0 }, 500)
            .wait(500)
    }
    /** 场景标题 */
    public static screenTitle(container, text?) {
        const txt = new egret.TextField();
        container.addChild(txt);
        txt.text = text || "场景标题";
    }
    /** 创建遮罩 */
    public static createMask(container: egret.DisplayObjectContainer, diyFun: Function = null):egret.Sprite {
        const miva = container;
        const mask = new egret.Sprite();
        mask.graphics.beginFill(0, .7);
        mask.graphics.drawRect(0, 0, miva.stage.stageWidth, miva.stage.stageHeight);
        mask.graphics.endFill();
        mask.touchEnabled = true;
        mask.addEventListener(egret.TouchEvent.TOUCH_BEGIN, Utils.superStopPropagation, miva);
        mask.addEventListener(egret.TouchEvent.TOUCH_MOVE, Utils.superStopPropagation, miva);
        mask.addEventListener(egret.TouchEvent.TOUCH_END, Utils.superStopPropagation, miva);
        mask.addEventListener(egret.TouchEvent.TOUCH_CANCEL, Utils.superStopPropagation, miva);
        mask.addEventListener(egret.TouchEvent.TOUCH_TAP, handler, miva);
        miva.addChild(mask);
        function handler(evt) {
            Utils.superStopPropagation(evt);
            if (diyFun) diyFun(evt)
        }
        return mask
    }
    /** 视图对象拖动获取坐标方法 */
    public static dragGetPatn(target: egret.DisplayObject, container, funObj?: { startFun?: Function, moveFun?: Function, endFun?: Function }, isDrag: boolean = true): void {
        /** 初始触摸点 */
        const startPoint: egret.Point = new egret.Point();
        const comput = new egret.Point();

        target.touchEnabled = true;
        target.addEventListener(egret.TouchEvent.TOUCH_BEGIN, touchBeginHandler, target);
        /** 开始触摸处理器 */
        function touchBeginHandler(evt) {
            startPoint.x = evt.stageX;
            startPoint.y = evt.stageY;
            target.addEventListener(egret.TouchEvent.TOUCH_MOVE, touchMoveHandler, target);
            target.addEventListener(egret.TouchEvent.TOUCH_END, touchEndHandler, target);

            container.setChildIndex(target, 99);
            /** 附加方法 */
            if (funObj && funObj.startFun) funObj.startFun(target, container);
        }
        /** 拖动处理器 */
        function touchMoveHandler(evt) {
            /** 附加方法 */
            if (funObj && funObj.moveFun) funObj.moveFun(target, container);
            /** 加入拖动开关, 默认开启拖动: 2019年3月11日 17:31:36 这里还有点小问题, 明天再改, 2019年3月12日 09:26:11 解决 */
            if (!isDrag) return;
            comput.x = evt.stageX - startPoint.x;
            comput.y = evt.stageY - startPoint.y;
            startPoint.x = evt.stageX;
            startPoint.y = evt.stageY;
            target.y += comput.y;
            target.x += comput.x;
        }
        /** 结束触摸处理器 */
        function touchEndHandler(evt) {
            console.log(target.x, target.y);
            target.removeEventListener(egret.TouchEvent.TOUCH_MOVE, touchMoveHandler, target);
            /** 附加方法 */
            if (funObj && funObj.endFun) funObj.endFun(target, container);
        }
    };
    /** 
     * 生成弹窗
     * 2019年6月6日 11:01:16 : 暂时先放置吧, 等项目完了回头来做
     */
    public static createAlert(target: egret.DisplayObjectContainer) {
        const miva = target;
        const container = new egret.Sprite();
        const alert_bg = Utils.createBitmapByName("rule_png");
        const close_btn = Utils.createBitmapByName("close_png");
        miva.stage.addChild(container);
        Utils.createMask(container, handler);

        function handler(evt) {
            miva.stage.removeChild(container);
        }
    }
    /**
     * 创建文字 
     */
    public static createText(text: string, path: { x: any, y: any }, target, isDrag: boolean = true, isCenter = false): egret.TextField {
        const miva = target;
        const textField = new egret.TextField();
        textField.text = text;
        textField.textColor = 0xf45050;
        textField.strokeColor = 0;
        textField.stroke = 3;
        textField.x = path.x;
        textField.y = path.y;
        textField.size = 55;
        textField.bold = true;
        miva.addChild(textField)
        if (isDrag) Utils.dragGetPatn(textField, miva);
        if (isCenter) Utils.anchorCenter(textField);
        return textField; // 这里返回实例, 即可进行二次操作
    }
}